package com.example.wangshuo.zyfypt_205ws.model;


import com.example.wangshuo.zyfypt_205ws.bean.ArticleBean;
import com.example.wangshuo.zyfypt_205ws.bean.VideoBean;
import com.example.wangshuo.zyfypt_205ws.iface.ArticleListener;
import com.example.wangshuo.zyfypt_205ws.iface.Articleiface;
import com.example.wangshuo.zyfypt_205ws.iface.Iface;
import com.example.wangshuo.zyfypt_205ws.iface.VideoListener;
import com.example.wangshuo.zyfypt_205ws.service.ArticleService;
import com.example.wangshuo.zyfypt_205ws.service.Service;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by wangshuo on 2017/4/19.
 */
//model，文章模块，实现articleiface接口
public class Model implements Iface {
   private Retrofit retrofit;
    private String BASEURL="http://amicool.neusoft.edu.cn/";
    public Model(){
        //实例化retrofit
        retrofit=new Retrofit.Builder().baseUrl(BASEURL).addConverterFactory(GsonConverterFactory.create()).build();
    }
    //获取文章数据
    @Override
    public void getvideoResultList(String mod, int page, String sessionID, final VideoListener listener) {
        Service service=retrofit.create(Service.class);
        Call<List<VideoBean>> call=service.getList(mod,page,sessionID);
        call.enqueue(new Callback<List<VideoBean>>() {
            @Override
            public void onResponse(Call<List<VideoBean>> call, Response<List<VideoBean>> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    listener.onResponse(response.body());
                }else
                    listener.onFail("on response fail");
            }
            @Override
            public void onFailure(Call<List<VideoBean>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });

    }
}
